package svc.elib.analysis;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;

import svc.elib.db.Author;
import svc.elib.db.Database;
import svc.elib.socnet.CentralityMetrics;
import svc.elib.socnet.Net;
import svc.elib.socnet.SocConstructor;

/**
 * Makes a vector describing author (for each author)
 * Vector contains parameters such as the number of coauthors, networked measures, etc.
 * # 2.8.2013
 * @author svc
 */
public class AuthorVector {

	private static String vectorHeader = 
		"name,numPapers,activitySpan,male,numCollaborators,CC,wbetweenness,wcloseness,weigenvector,wpagerank," +
	    "betweenness,closeness,eigenvector,pagerank";
	
	private static HashMap<String, AV> map = 
		new HashMap<String, AV>();
	
	private static class AV {
		double wb, wc, we, wp;
		double b, c, e, p;
		
		public void setw(double wb, double wc, double we, double wp) {
			this.wb = wb;
			this.wc = wc;
			this.we = we;
			this.wp = wp;
		}
		
		public void set(double b, double c, double e, double p) {
			this.b = b;
			this.c = c;
			this.e = e;
			this.p = p;
		}
		
		public String getCentralityVector() {
			return wb + "," + wc + "," + we + "," + wp + "," + b + "," + c + "," + e + "," + p;
		}
	}
	
	private static String buildVector(Net net, Author a) {
		StringBuilder sb = new StringBuilder();
		sb.append(a.getName()).append(",");
		sb.append(a.getNumPapers()).append(",");
		sb.append(a.getActivitySpan()).append(",");
		sb.append(a.isMale()).append(",");
		
		int numCollaborators = net.getGraph().degree(a);
		sb.append(numCollaborators).append(",");
		sb.append(net.getCC(a)).append(",");
		
		AV av = map.get(a.getName());
		sb.append(av.getCentralityVector());
		
		return sb.toString();
	}
	
	public static void main(String[] args) 
		throws IOException
	{
		Database db = new Database("eLibData.csv", 1932, 2011);
		System.out.println(db.info());
		SocConstructor soc = new SocConstructor(db);
		Net net = soc.getNet();
		
		CentralityMetrics cm = new CentralityMetrics(net);
		cm.computeBetweeness(true);
		cm.computeCloseness(true);
		cm.computeEigenvector(true);
		cm.computePageRank(true);
	
		Iterator<Author> ait = net.getGraph().getVertices().iterator();
		while (ait.hasNext()) {
			Author a = ait.next();
			AV av = new AV();
			av.setw(cm.getBetweenness(a.getName()), cm.getCloseness(a.getName()), cm.getEigenvector(a.getName()), cm.getPageRank(a.getName()));
			map.put(a.getName(), av);
		}
		
		cm = new CentralityMetrics(net);
		cm.computeBetweeness(false);
		cm.computeCloseness(false);
		cm.computeEigenvector(false);
		cm.computePageRank(false);
		
		ait = net.getGraph().getVertices().iterator();
		while (ait.hasNext()) {
			Author a = ait.next();
			AV av = map.get(a.getName());
			av.set(cm.getBetweenness(a.getName()), cm.getCloseness(a.getName()), cm.getEigenvector(a.getName()), cm.getPageRank(a.getName()));
		}
		
		PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("results/AuthorVectors.csv")));
		pw.println(vectorHeader);
		
		ait = net.getGraph().getVertices().iterator();
		while (ait.hasNext()) {
			Author a = ait.next();
			String vector = buildVector(net, a);
			System.out.println(vector);
			pw.println(vector);
		}
		
		pw.close();
	}	
}
